import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Application {
    static Logger logger = Logger.getLogger("VoiceClass");
    static ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
    static Dispatch sapo = sap.getObject();
    static Map<String, Integer> data_map = new LinkedHashMap<>();

    public static void initData() {
//            近光灯
        data_map.put("请开启前照灯", 1);
        data_map.put("夜间路口直行", 1);
        data_map.put("夜间与机动车会车", 1);
        data_map.put("夜间在窄路与非机动车会车", 1);
        data_map.put("夜间在窄桥与非机动车会车", 1);
        data_map.put("夜间同方向近距离跟车行驶", 1);
        data_map.put("夜间在有路灯、照明良好的条件下行驶", 1);
//            远光灯
        data_map.put("请将前照灯变换成远光灯", 2);
        data_map.put("夜间在没有路灯、照明不良的条件下行驶", 2);
//            交替使用远近光灯
        data_map.put("夜间通过急弯、坡路", 3);
        data_map.put("夜间通过坡路、拱桥", 3);
        data_map.put("夜间通过急弯、拱桥", 3);
        data_map.put("夜间通过拱桥、人行横道", 3);
        data_map.put("夜间通过没有交通信号灯控制的路口", 3);
        data_map.put("夜间超越前方车辆", 3);
//            近光灯+对应转向灯
        data_map.put("夜间路口左转弯", 41);
        data_map.put("夜间路口右转弯", 42);
//            示宽灯+危险警示灯
        data_map.put("路边临时停车", 5);
        data_map.put("夜间在道路上发生故障妨碍交通又难以移动的", 5);
    }

    static void disruptDataMap() {
        List<Map.Entry<String, Integer>> list = new ArrayList<>(data_map.entrySet());
        Collections.shuffle(list);

        data_map = list.stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue,
                        LinkedHashMap::new
                ));
        logger.info("打乱数据完成");
    }

    static {
        initData();
        logger.info("初始化数据完成");
    }

    public static void main(String[] args) {
        //简陋版
        disruptDataMap();
        Set<String> strings = data_map.keySet();
        System.out.print("\n");
        System.out.println("近光灯：1");
        System.out.println("远光灯：2");
        System.out.println("交替使用远近光灯：3");
        System.out.println("近光灯+左转向灯：41");
        System.out.println("近光灯+右转向灯：42");
        System.out.println("示宽灯+危险警示灯：5\n");
        say("下面进行夜间灯光模拟");
        Scanner scan = new Scanner(System.in);
        for (String s : strings) {
            say(s);
            System.out.println("-------------");
            long start = System.currentTimeMillis();
            int in = scan.nextInt();
            if (in != data_map.get(s)) {
                say("回答错误，结束");
                say("正确答案：" + data_map.get(s));
                System.exit(0);
            }
            long end = System.currentTimeMillis();
            System.out.println("用时：" + (end - start) / 1000 + "s");
            if (end - start > 5000) {
                say("回答超时，结束");
                System.exit(0);
            }
        }
        sapo.safeRelease();
        sap.safeRelease();
        logger.info("程序结束");
    }

    static void say(String str) {
        try {
            // 音量 0-100
            sap.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            sap.setProperty("Rate", new Variant(1));
            // 执行朗读
            Dispatch.call(sapo, "Speak", new Variant(str));
        } catch (Exception e) {
            logger.severe(e.getMessage());
        }
    }
}